//
//  xinwenImageViewTableViewCell.swift
//  BusinessIOS
//
//  Created by Rain on 2019/1/8.
//  Copyright © 2019 XingJian Li. All rights reserved.
//

import UIKit
import Photos

protocol xinwenDeleagte {
    func xinwenDeleagte(text: Array<Any>,  indexs:NSInteger)
}

class xinwenImageViewTableViewCell: UITableViewCell {
    
    
    var delegate: xinwenDeleagte?
    var imageviews :UIImageView!

    var imageArray = [Any]()
    
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        
    }
    
    
    func xinwenImageViewTableViewCell(model: Array<Any>) {
        
        
        imageArray = model
        
        
        
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        for i in 0..<imageArray.count {
            
            print("-------\(imageArray.count)")
            
            
            
            self.imageviews = UIImageView()
            
            
            self.imageviews.tag = i
            self.imageviews.contentMode = .scaleAspectFill
            self.imageviews.clipsToBounds = true
            
            let row = i / 3
            let col = i % 3
            
            let x:CGFloat = self.contentView.frame.size.width/3 * CGFloat(col)
            
            let y:CGFloat = 70 * CGFloat(row)
            
            self.imageviews.frame = CGRect(x:x, y:y, width:CGFloat(self.contentView.frame.size.width / 3), height:70)
            
            self.imageviews.image = imageArray[i] as? UIImage
            
            
            
            self.contentView.addSubview(self.imageviews)
            
            
            let tap = UITapGestureRecognizer(target: self, action: #selector(self.onClickedImageEvents(gest:)))
            
            
            
            self.imageviews.addGestureRecognizer(tap)
            
            self.imageviews.isUserInteractionEnabled = true
            
        }
        
        
        
        
    }
    @objc func onClickedImageEvents(gest:UITapGestureRecognizer) {
        
        
        
        
        
        //图片索引
        let index = gest.view!.tag
        //        //进入图片全屏展示
        //        let previewVC = ImagePreviewVC(images: self.imageArray as! [UIImage], index: index)
        
        
        delegate?.xinwenDeleagte(text: self.imageArray, indexs: index)
        
        
        
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
    }
    
    
    
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
